package com.jeeplus.test.jlnu.studentStatusInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AllInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.ExcelMapper;
import com.jeeplus.test.jlnu.archivalInfo.domain.entity.ArchParmerter;
import com.jeeplus.test.jlnu.fieldConfig.domain.FieldConfigEntity;
import com.jeeplus.test.jlnu.fieldConfig.mapper.StudentStatusFieldConfigMapper;
import com.jeeplus.test.jlnu.studentStatusInfo.domain.entity.StudentStatusInfo;
import com.jeeplus.test.jlnu.studentStatusInfo.domain.vo.StudentStatusInfoReqVo;
import com.jeeplus.test.jlnu.studentStatusInfo.mapper.StudentStatusInfoMapper;
import com.jeeplus.test.jlnu.studentStatusInfo.service.StudentStatusInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentStatusInfoServiceImpl implements StudentStatusInfoService {

    @Autowired
    private StudentStatusInfoMapper studentStatusInfoMapper;

    @Autowired
    private ExcelMapper excelMapper;

    @Autowired
    private StudentStatusFieldConfigMapper studentStatusFieldConfigMapper;

    @Override
    public Page<StudentStatusInfo> getStudentStatusInfoPage(ArchParmerter studentStatusInfoReqVo, Page<StudentStatusInfo> page) {
        // 1. 获取分页结果和字段配置信息
        Page<StudentStatusInfo> studentStatusInfoPage = studentStatusInfoMapper.getStudentStatusInfoPage(studentStatusInfoReqVo, page);

        return studentStatusInfoPage;
    }

    @Override
    public int updateStudentStatus(StudentStatusInfoReqVo studentStatusInfoReqVo) {
        return studentStatusInfoMapper.updateStudentStatus(studentStatusInfoReqVo);
    }

    @Override
    public void addStudentInfo(StudentStatusInfoReqVo studentStatusInfoReqVo) {
        List<AdmissionInfo> admissionInfos = new ArrayList<>();
        AdmissionInfo admissionInfo = new AdmissionInfo();
        admissionInfo.setStudentId(studentStatusInfoReqVo.getStudentId());
        admissionInfo.setExpectedGraduationDate(studentStatusInfoReqVo.getGraduationDate());
        admissionInfo.setGradation(studentStatusInfoReqVo.getGradation());
        admissionInfos.add(admissionInfo);
        // 插入毕业信息表（只插入学号）
//        excelMapper.saveThirdData(admissionInfos);
        // 插入就业信息表（只插入学号）
        excelMapper.saveFourth(admissionInfos);
        // 插入学位信息表（只插入学号）
        excelMapper.saveFifth(admissionInfos);
        // 插入学历信息表（只插入学号）
        excelMapper.saveSixth(admissionInfos);
        // 插入转递（邮寄信息表）
        excelMapper.saveSeventh(admissionInfos);
        // 档案信息表
//        excelMapper.saveEighth(admissionInfos);
        excelMapper.saveFourteenth(admissionInfos);
        excelMapper.saveFifteenth(admissionInfos);
         studentStatusInfoMapper.addStudentInfo(studentStatusInfoReqVo);
        // 创建两个列表来存储不同 flag 的对象
        List<AdmissionInfo> flagOneList = new ArrayList<>();
        List<AdmissionInfo> otherFlagList = new ArrayList<>();

        for (AdmissionInfo admissionInfoss : admissionInfos) {
            if(admissionInfoss.getGradation().equals("本科")){
                admissionInfoss.setFlag("1");
            }else if(admissionInfoss.getGradation().equals("硕士")){
                admissionInfoss.setFlag("2");
            }else {
                admissionInfoss.setFlag("3");
            }

            if ("1".equals(admissionInfoss.getFlag())) {
                flagOneList.add(admissionInfoss);
            } else {
                otherFlagList.add(admissionInfoss);
            }
        }

        // 对于 flag 为 1 的对象调用保存方法
        if (!flagOneList.isEmpty()) {
            excelMapper.saveNinth(flagOneList);
            excelMapper.saveTenth(flagOneList);
            excelMapper.saveEleth(flagOneList);
            excelMapper.saveThirteenth(flagOneList);
        }

        // 对于 flag 为 2 或其他的对象调用保存方法
        if (!otherFlagList.isEmpty()) {
            excelMapper.saveTwelfth(otherFlagList);
            excelMapper.saveSixteenth(otherFlagList);
            excelMapper.saveSeventeenth(otherFlagList);
            excelMapper.saveEighteenth(otherFlagList);
        }
    }
}
